package com.pen.interfece.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pen.interfece.Context.BaseContext;
import com.pen.interfece.mapper.DisciplinaryMapper;
import com.pen.interfece.mapper.UserMapper;
import com.pen.interfece.pojo.PageResult;
import com.pen.interfece.pojo.dto.illPageQueryDTO;
import com.pen.interfece.pojo.entity.*;
import com.pen.interfece.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DisciplinaryMapper disciplinaryMapper;
    @Override
    public userLogin login(userLogin userlogin) {
       userLogin user= userMapper.getByAccount(userlogin);
       return user;
    }


    public PageResult page(illPageQueryDTO ill) {
        //开始分页查询，插件执行
        PageHelper.startPage(ill.getPage(),ill.getPageSize());
        Page<ill> page=userMapper.selectIll(ill);
        long total = page.getTotal();
        List<ill> records = page.getResult();

        return new PageResult(total,records);
    }

    @Override
    public List<Maintenance> pageMt() {
        //查询到的是账号的id
        Long id= BaseContext.getCurrentId();
        //先把账号查出来
        String account=userMapper.pageQuery(id);
        log.info(account);
        // 找到 '@' 符号的位置
        int atIndex = account.indexOf('@');
        // 截取 '@' 符号后面的部分
        String result = account.substring(atIndex + 1);
        int StuId= Integer.parseInt(result);
        List<Maintenance> maintenances=disciplinaryMapper.pageMt(StuId);
        return maintenances;
    }

    @Override
    public void register(Admin admin) {
        admin.setTime(LocalDateTime.now());
        userMapper.register(admin);
    }

    @Override
    public void save(ill i) {
        userMapper.save(i);
    }

    @Override
    public void update(Admin admin) {
        userMapper.update(admin);
    }
}
